package com.jintoufs.web.action.invoice;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jhj.common.constants.Constants;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.QueryRule;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.bill.BillService;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.web.action.common.JxlsHandler;
import com.jintoufs.web.action.support.JqgridPage;
import org.apache.commons.lang.StringUtils;
import org.cosmos.modules.exception.ServiceException;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @description 开票信息管理
 * @author zhenghq
 * @date 2018-02-28 17:08
 */
@Controller
@RequestMapping("/invoice")
public class InvoiceAction {

    @Resource
    BillService billService;

    @RequestMapping("/list")
    public String list(Model model, String company_name , String tel, String bank_name) {
        model.addAttribute("company_name",company_name);
        model.addAttribute("tel",tel);
        model.addAttribute("bank_name",bank_name);
        return "invoice/list";
    }
    /**
     * 查询开票信息
     */
    @RequestMapping("/query")
    @ResponseBody
    public JqgridPage query(int page, int rows, String sidx, String sord, String filters,
                            String company_name , String tel, String bank_name) {

        PageHelper.startPage(page, rows);
        QueryFilters filters_ = StringUtils.isEmpty(filters)?new QueryFilters()
                : JSON.parseObject(filters, QueryFilters.class);
        if(StringUtils.isNotEmpty(company_name)){
            QueryRule role = new QueryRule();
            role.setField("company_name");
            role.setOp("cn");
            role.setData(company_name);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(tel)){
            QueryRule role = new QueryRule();
            role.setField("tel");
            role.setOp("cn");
            role.setData(tel);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(bank_name)){
            QueryRule role = new QueryRule();
            role.setField("bank_name");
            role.setOp("cn");
            role.setData(bank_name);
            filters_.getRules().add(role);
        }

            QueryRule role1 = new QueryRule();
            role1.setField("ods.status");
            role1.setOp("eq");
            role1.setData(com.jintoufs.domain.constants.Constants.ORDER_STATUS_OVER);
            filters_.getRules().add(role1);

        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Map<String,Object>> list = billService.getByFilter(filters_);
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(), pageInfo.getList());
    }

    @RequestMapping("/auth")
    @ResponseBody
    @SystemControllerLog(description = "审核开票信息")
    public AjaxMsg auth(String id,String type){
        AjaxMsg msg = new AjaxMsg();
        try {
            billService.auth(id,type);
            msg.setCode(AjaxMsg.SUCCESS);
            if ("1".equals(type)){
                msg.setMessage("审核开票信息成功");
            }else {
                msg.setMessage("驳回开票信息成功");
            }
        } catch (ServiceException se){
            msg.setCode(AjaxMsg.FAILURE);
            msg.setMessage(se.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            msg.setCode(AjaxMsg.FAILURE);
            msg.setMessage("审核开票信息失败");
        } finally {
        }
        return msg;
    }

    /**
     * 下载
     */
    @RequestMapping("/download")
    @ResponseBody
    public void download(HttpServletRequest request, HttpServletResponse response, String sidx, String sord, String filters,
                         String company_name , String tel, String bank_name) {
        PageHelper.startPage(1, 1000);
        QueryFilters filters_ = StringUtils.isEmpty(filters)?new QueryFilters()
                : JSON.parseObject(filters, QueryFilters.class);
        if(StringUtils.isNotEmpty(company_name)){
            QueryRule role = new QueryRule();
            role.setField("company_name");
            role.setOp("cn");
            role.setData(company_name);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(tel)){
            QueryRule role = new QueryRule();
            role.setField("tel");
            role.setOp("cn");
            role.setData(tel);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(bank_name)){
            QueryRule role = new QueryRule();
            role.setField("bank_name");
            role.setOp("cn");
            role.setData(bank_name);
            filters_.getRules().add(role);
        }
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Map<String,Object>> list = billService.getByFilter(filters_);
        try {
            JxlsHandler.report(request,response,"订单开票流水-下载模板.xlsx",list);
        }catch (IOException e){

        }
    }
}
